package com.qf.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description: 公用常量
 * @Data: 2022/9/13 15:36
 * @Pacakge: com.qf.aop
 * @ClassName: MyAspect
 * @Author: fan
 * 配置日志
 */
//声明当前类为一个切面类
@Aspect
@Component
public class MyAspect {
    //声明日志
    private static final Logger logger = (Logger) LoggerFactory.getLogger(MyAspect.class);

    //标记切点
    @Pointcut(value = "execution(* *.*.*.impl.*.*(..))")
    public void pointcut(){

    }
    //环绕配置
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        logger.debug("开始记录日志"+new Date());
        //获取执行方法
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        logger.debug("执行的方法为:"+signature.getMethod());
        Object proceed = null;
        try {
            proceed = proceedingJoinPoint.proceed();
            logger.debug("执行的方法为:"+signature.getMethod()+",执行完成");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error("执行的方法为"+signature.getMethod()+"出现异常:"+throwable.getMessage());
        }finally{
            logger.debug("执行时间为:"+new Date());
        }
        return proceed;
    }

}
